package com.isp.org.server.interfaces.http.group;

import com.isp.common.model.vo.PageResult;
import com.isp.common.model.vo.Result;
import com.isp.org.server.application.facade.GroupFacade;
import com.isp.org.server.interfaces.model.group.request.CreateGroupRequest;
import com.isp.org.server.interfaces.model.group.request.UpdateGroupRequest;
import com.isp.org.server.interfaces.model.group.response.CreateGroupResponse;
import com.isp.org.server.interfaces.model.group.response.GroupDetailResponse;
import com.isp.org.server.interfaces.model.group.response.GroupListResponse;
import com.isp.org.server.interfaces.model.group.response.UpdateGroupResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/group")
@Api("group")
@Slf4j
public class GroupController {

    @Autowired
    private GroupFacade groupFacade;

    @ApiOperation(value = "新增用户组", notes = "新增一个用户组")
    @ApiImplicitParam(name = "groupForm", value = "新增用户组form表单", required = true, dataType = "GroupForm")
    @PostMapping
    public Result<Boolean> add(@Valid @RequestBody CreateGroupRequest request) {
        return Result.success(groupFacade.add(request));
    }

    @ApiOperation(value = "删除用户组", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "用户组ID", required = true, dataType = "string")
    @DeleteMapping(value = "/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.success("删除成功", groupFacade.delete(id));
    }

    @ApiOperation(value = "修改用户组", notes = "修改指定用户组信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户组ID", required = true, dataType = "string"),
            @ApiImplicitParam(name = "groupForm", value = "用户组实体", required = true, dataType = "GroupForm")
    })
    @PutMapping(value = "/{id}")
    public Result<Boolean> update(@PathVariable Long id, @Valid @RequestBody UpdateGroupRequest request) {
        return Result.success(groupFacade.update(id, request));
    }

    @ApiOperation(value = "获取用户组", notes = "获取指定用户组信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "用户组ID", required = true, dataType = "string")
    @GetMapping(value = "/{id}")
    public Result<GroupDetailResponse> get(@PathVariable Long id) {
        log.debug("get with id:{}", id);
        return Result.success(groupFacade.get(id));
    }

    @ApiOperation(value = "查询用户组", notes = "根据条件查询用户组信息，简单查询")
    @ApiImplicitParam(paramType = "query", name = "name", value = "用户组名称", required = true, dataType = "string")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @GetMapping
    public Result<List<GroupListResponse>> query(@RequestParam String name) {
        log.debug("query with name:{}", name);
        return groupFacade.query(name);
    }

    @ApiOperation(value = "根据用户名查询用户组列表", notes = "根据用户名查询用户组列表")
    @ApiImplicitParam(paramType = "query", name = "name", value = "用户名称", required = true, dataType = "string")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @GetMapping(value = "/user/{username}")
    public Result<List<GroupListResponse>> queryByUsername(@PathVariable String username) {
        log.debug("query with username:{}", username);
        return groupFacade.queryByUserName(username);
    }

    @ApiOperation(value = "根据父id查询用户组", notes = "根据父id查询用户组列表")
    @ApiImplicitParam(paramType = "path", name = "id", value = "用户组父ID", required = true, dataType = "string")
    @GetMapping(value = "/parent/{id}")
    public Result<List<GroupListResponse>> search(@PathVariable Long id) {
        log.debug("query with parent id:{}", id);
        return groupFacade.queryByParentId(id);
    }
}